import React from 'react';
import { IRouteComponentProps, RequestConfig, history } from 'umi';
import { getCookie, setCookie } from '@/utils/auth';
const baseURL = 'http://157.122.54.189:9060/'
let isModal = false;


export const request: RequestConfig = {
  timeout: 100000,
  errorConfig: {},
  middlewares: [],
  // 请求拦截器
  requestInterceptors: [(url, options) => {
    
    const authorization = getCookie() as string;
    if (!/http/.test(url)) {
      url = baseURL + url;
    }
    let headers = options.headers;
    if (authorization){
      headers = { ...headers, authorization }
    }
    return {
      url,
      options: { ...options, interceptors: true, headers },
    };
  }],
  // 响应拦截器
  responseInterceptors: [(response, options) => {

    return response;
  }],
};



// 导航守卫
const whiteList = ['/'];
export function onRouteChange({location, matchedRoutes }: IRouteComponentProps) {
  const authorization = getCookie() as string;
  if (matchedRoutes.length) {
    document.title = matchedRoutes[matchedRoutes.length - 1].route.title || '';
  }
  // 登陆态拦截
  if (!authorization){
    if (whiteList.indexOf(location.pathname) == -1) {
      history.replace(`/`)
    }
  }
}